package com.quanquan.user.service.statisticsReport;

import com.quanquan.user.entity.SelfHelpIntegral;
import com.quanquan.user.repository.SelfHelpIntegralRepository;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import utils.DateUtils;

import javax.persistence.criteria.Predicate;
import java.math.BigInteger;
import java.util.*;

/**
 * 功能描述:商铺排名
 *
 * @ClassName: StoreConsuneRankingService
 * @Author: xiaoqianh丶
 * @Date: 2019-05-29 18:21
 * @Version: V1.0
 */
@Service
public class StoreConsuneRankingService {

    private final SelfHelpIntegralRepository selfHelpIntegralRepository;



    public StoreConsuneRankingService(SelfHelpIntegralRepository selfHelpIntegralRepository) {
        this.selfHelpIntegralRepository = selfHelpIntegralRepository;

    }


    /**
     * 按时间查询（报表使用）（获得）
     * @param startTime
     * @param endTime
     * @return
     */
    public List<SelfHelpIntegral> statisticalReportsReceive(Date startTime, Date endTime) {
        return selfHelpIntegralRepository.findAll((Specification<SelfHelpIntegral>) (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();

            predicates.add(cb.greaterThanOrEqualTo(root.get("createTime").as(Date.class),startTime));
            predicates.add(cb.lessThanOrEqualTo(root.get("createTime").as(Date.class), endTime));
            predicates.add(cb.equal(root.get("applicationStatus").as(SelfHelpIntegral.Application.class), SelfHelpIntegral.Application.successpass));
            return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        });
    }

    public Map<String, Object> findStoreByNumber(Date startTime, Date endTime) {
        String startDate = DateUtils.parseDate2Str("yyyy-MM-dd HH:mm:ss", startTime);
        String endDate = DateUtils.parseDate2Str("yyyy-MM-dd HH:mm:ss", endTime);
        List<Object[]> storeByNum = selfHelpIntegralRepository.findStoreByNum(startDate, endDate);
        Map<String, Object> result = new LinkedHashMap<>();
        for(Object[] objects : storeByNum){
            result.put((String) objects[1],(BigInteger) objects[0]);
        }

        return result;
    }



    public Map<String, Double> findStoreByIntegral(Date startTime, Date endTime) {
        String startDate = DateUtils.parseDate2Str("yyyy-MM-dd HH:mm:ss", startTime);
        String endDate = DateUtils.parseDate2Str("yyyy-MM-dd HH:mm:ss", endTime);
        List<Object[]> storeByNum = selfHelpIntegralRepository.findStoreByIntegral(startDate, endDate);
        Map<String, Double> result = new LinkedHashMap<>();
        for(Object[] objects : storeByNum){
            result.put((String) objects[1],(Double) objects[0]);
        }
        return result;
    }

    public Map<String, Double> findStoreByMoney(Date startTime, Date endTime) {
        String startDate = DateUtils.parseDate2Str("yyyy-MM-dd HH:mm:ss", startTime);
        String endDate = DateUtils.parseDate2Str("yyyy-MM-dd HH:mm:ss", endTime);
        List<Object[]> storeByNum = selfHelpIntegralRepository.findStoreByMoney(startDate, endDate);
        Map<String, Double> result = new LinkedHashMap<>();
        for(Object[] objects : storeByNum){
            result.put((String) objects[1],(Double) objects[0]);
        }
        return result;
    }


}
